package com.wenlei;

import com.wenlei.listener.MyServletContextAttributeListener;
import com.wenlei.servlet.FinalServlet;
import com.wenlei.servlet.ForwardServlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.Set;

/**
 * 通过API编程，设置2个Servlet，和2个Filter，2个listener
 * forwardServlet 内部会forward到 FinalServlet
 * @author wenl
 */
public class InitWebApplicationInitializer implements ServletContainerInitializer {

    public static final String FINALINFO= "/finalInfo" ;

    @Override
    public void onStartup(Set<Class<?>> c, ServletContext servletContext) throws ServletException {
        // 设置过滤器
        // 设置Servlet
        ServletRegistration.Dynamic forwordto =  servletContext.addServlet("ForwardServlet", ForwardServlet.class);
        forwordto.addMapping("/forwordto");
        ServletRegistration.Dynamic finalServlet =   servletContext.addServlet("FinalServlet", FinalServlet.class);
        finalServlet.addMapping("/finalInfo");

        // 设置2个监听器
        servletContext.addListener( MyServletContextAttributeListener.class );
        servletContext.addListener( MyServletContextAttributeListener.class );
        // 有一个过滤器，使用的是注解 EncodingFilter ，用于解决中文乱码
    }
}
